<?php
namespace admin\widgets;

use app\models\AdminMenu;
use yii;
use yii\widgets\Menu;

class AdminMenuWidget extends Menu {

    public $encodeLabels = FALSE;
    public $activateParents = TRUE;
    public $options = ['id' => 'menu', 'class' => 'menu'];
    public $submenuTemplate = "\n<ul class=\"sub-menu\">\n{items}\n</ul>\n";

    public function init() {
        parent::init();

        $this->route = str_replace('index', '', Yii::$app->controller->uniqueId);

        $model = new AdminMenu();
        $this->items = $model->formatterMenu();
    }

    protected function isItemActive($item) {
        if (isset($item['url']) && is_array($item['url']) && isset($item['url'][0])) {
            $route = Yii::getAlias($item['url'][0]);
            if ($route[0] !== '/' && Yii::$app->id) {
                $route = Yii::$app->controller->module->getUniqueId() . '/' . $route;
            }
            if (trim($route, '/') !== $this->route) {
                return FALSE;
            }
            unset($item['url']['#']);
            if (count($item['url']) > 1) {
                $params = $item['url'];
                unset($params[0]);
                foreach ($params as $name => $value) {
                    if ($value !== NULL && (!isset($this->params[$name]) || $this->params[$name] != $value)) {
                        return FALSE;
                    }
                }
            }
            return TRUE;
        }
        return FALSE;
    }
}